import umap
from sklearn.datasets import load_digits
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
import umap.umap_

# 手写数字数据集
digits = load_digits()
data = digits.data
target = digits.target

# 使用UMAP进行降维
reducer = umap.umap_.UMAP()
embedding = reducer.fit_transform(data)

# 可视化降维后的数据
plt.scatter(embedding[:, 0], embedding[:, 1], c=target, cmap='Spectral', s=5)
plt.colorbar()
plt.show()


# 生成一个包含两个聚类的模拟数据集
X, y = make_moons(n_samples=2000, noise=0.05)

# 使用 UMAP 进行降维
reducer = umap.UMAP(n_components=2)
embedding = reducer.fit_transform(X)

# 可视化降维结果
plt.figure(figsize=(8, 6))
plt.scatter(embedding[:, 0], embedding[:, 1], c=y, cmap='viridis')
plt.title('UMAP Projection of Moon Data')
plt.show()